import {action, autorun, computed, observable, runInAction} from "mobx";
import {BaseResInterface, BaseResInterfaceWithData, sendGetRequest, sendPostRequest} from "../../../net";
import {realm} from "../../../dao";
import {
    ShippingAddressInterface,
    UserModelInterface,
    UserRealInfoInterface,
    UserVipInfoInterface
} from "../../../net/instance/CommenModelInstance";
import {defaultObj, excludeEmpty} from "../../../util";
import {Banner} from "../main/banner";
import {Collection} from "realm";
import * as _ from 'lodash'

export interface validPhoneResInterface extends BaseResInterface {
    data: {
        verificationCode: number
    }
}

interface UserResInterface<T> extends BaseResInterface {
    data: T
}


export class UserStore {
    @observable verificationCode: string;
    @observable shareCode: string;
    @observable user: UserModelInterface = defaultObj;
    @observable recommender: UserModelInterface = defaultObj;
    dbUserStore: Collection<UserModelInterface>;

    @action.bound
    async getVerificationCode(phone: number) {
        const {data, code} = await sendPostRequest<validPhoneResInterface>("user/validPhone", {
            phone
        });

        data && runInAction(() => {
            this.verificationCode = data.verificationCode + "";
        });
        return code;
    }

    @action.bound
    async loginAction(phone: number) {
        let {data} = await sendPostRequest<UserResInterface<UserModelInterface>>("user/phoneLogin", {
            phone
        });
        data.phone+="";
        //保存用户信息到本地
        realm.write(() => {
            realm.create<UserModelInterface>("User",data,true);
        });
        runInAction(() => {
            this.user = data;
        });
    }

    @action.bound
    async bindShareCode(shareCode: string) {
        const res = await sendPostRequest<UserResInterface<UserModelInterface>>("user/bindShareCode", {
            shareCode,
            userId: this.user.id
        });
        if (res.code === 200) {
            const {data} = res;
            //保存用户信息到本地
            realm.write(() => {
                realm.create<UserModelInterface>("User", data, true);
            });
            runInAction(() => {
                this.user = data;
            });
        }
        return res;
    }


    @action.bound
    async loginByWechatAction(code: string) {
        const {data} = await sendPostRequest<UserResInterface<UserModelInterface>>("user/wechatLogin", {
            code
        });
        //保存用户信息到本地
        realm.write(() => {
            realm.create<UserModelInterface>("User", data);
        });
        runInAction(() => {
            this.user = data;
        });
    }

    @action.bound
    async refreshUserData() {
        const {data} = await sendGetRequest<UserResInterface<UserModelInterface>>("user/getUserInfoById", {
            id: this.user.id
        });
        //保存用户信息到本地
        realm.write(() => {
            realm.create<UserModelInterface>("User", data);
        });
        runInAction(() => {
            this.user = data;
        });
    }

    @action.bound
    async updateUserInfo(params: UserModelInterface) {
        const {data} = await sendPostRequest<UserResInterface<UserModelInterface>>("user/updateUserInfo", {
            ...params,
            id: this.user.id,
        });
        //保存用户信息到本地
        realm.write(() => {
            realm.create<UserModelInterface>("User", data);
        });
        runInAction(() => {
            this.user = data;
        });
    }

    @action.bound
    async getRecommender() {
        if (!_.isEmpty(this.user)) {
            this.recommender = this.dbUserStore.filtered(`id = ${this.user.recommenderId}`)[0];
            const {data} = await sendGetRequest<UserResInterface<UserModelInterface>>("user/getUserInfoById", {
                id: this.user.recommenderId,
            });
            if (!_.isEmpty(data)) {
                //保存用户信息到本地
                realm.write(() => {
                    realm.create<UserModelInterface>("User", data);
                });
                runInAction(() => {
                    this.recommender = data;
                });
            }
        }

    }

    @action.bound
    async logOut() {
        realm.write(() => {
            realm.delete(this.dbUserStore);
        });
    }

    @action.bound
    async getShareCode() {
        const {data} = await sendPostRequest<UserResInterface<string>>("user/getShareCode", {
            id: this.user.id,
        });
       runInAction(()=>{
           this.shareCode = data;
       })
    }

    constructor() {
        this.dbUserStore = realm.objects<UserModelInterface>("User");
        this.user = this.dbUserStore[0];
    }


}

const UserStoreInstance = new UserStore();
export default UserStoreInstance;